<?php
session_start();
if(!isset($_SESSION['id'])){
    header('Location: login.php');
    exit;
}

include_once("../controller/predio_controller_class.php");

$objCampus = new CampusController();
$listCampus = $objCampus->selecionar();
?>

<!DOCTYPE html>
<html lang="en" class="no-js">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
        <title>::: SOS UNITAU -- Sistema de ordem de serviço</title>

        <link rel="shortcut icon" href="img/logo.jpg">
        <link rel="stylesheet" type="text/css" href="css/normalize.css" />
        <link rel="stylesheet" type="text/css" href="css/demo.css" />
        <link rel="stylesheet" type="text/css" href="css/icons.css" />
        <link rel="stylesheet" type="text/css" href="css/style2.css" />
        <link rel="stylesheet" type="text/css" href="css/StylePage.css" />
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <link rel="stylesheet" type="text/css" href="css/component.css" />
        <script type="text/javascript" src="js/jquery1.6.min.js"></script>
        <script src="js/jquery.maskedinput-1.3.min.js"></script>

    <script>
        $(document).ready(function () {
            $('#novoPredio').click(function () {

                var camp = $('#txtCampus').val().trim();
                var predio = $('#txtPredio').val().trim();

                if (camp == 0) {
                    alert('Preencha o campus');
                } else if (predio == "") {
                    alert('Preencha o predio');
                } else {
                    var c = window.opener.$('#txtCampus').val();
                    
                    $.post("../funcoes/novo_predio.php", {camp: camp, predio: predio}, function (retorno) {
//                        if (retorno.length) {
//                                alert
//                        }
                        
                        if(c === camp){
                            window.opener.$('#txtPredio').find('option').remove();

                            window.opener.$('#txtPredio').append("<option value='0'>Selecione</option>");
                        
                            $.each(retorno.predios, function (key, value) {
//                                alert(value.id + " - " + value.nome);
                                window.opener.$('#txtPredio').append("<option value='" + value.id + "'>" + value.nome + "</option>");

                            });
                        }
                        alert("Prédio cadastrado com Sucesso!");
                        window.self.close();
                    }, "json").fail(function () {
                        alert("erro");
                    });
                }
            });
        });
    </script>


    </head>
    <body style="overflow-x: hidden">
        <form name="adicionarPredio" id="adicionarPredio" method="post" enctype="multipart/form-data" action="" class="style-form">

            <fieldset style="width:30px;">
                <legend>Informações do logradouro </legend>
                <table width="800px" class="table-form">
                    <tr>
                        <td> 
                            <label for="txtCampus"> Campus: </label> 
                        </td>
                        <td> 
                            <select name="txtCampus" id="txtCampus" placeholder="Escolha">
                                <option value="0">Selecione</option>
                                <?php foreach ($listCampus as $campus) { ?>
                                    <option value="<?= $campus->getId() ?>"><?= $campus->getNome() ?></option>
                                <?php } ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td> 
                            <label for="txtPredio"> Prédio: </label>
                        </td>
                        <td> 
                            <input name="txtPredio" id="txtPredio" type="text" required placeholder="Ex: Informática" />
                        </td>
                    </tr>


                </table>
                <input type="button" value="Gravar" id="novoPredio" class="botao"  style="display:inline-block; margin-left:50%; margin-top:3%;" />
                <input type="button" value="Cancelar" style="margin-left:5%;" onClick="javascript:c = window.confirm('Deseja Realmente cancelar?');
                        if (c == true) {
                            self.close();
                        } else {
                        }" class="botao" />    
            </fieldset>

        </form>


    </body>
</html>